﻿using System;
using System.Text;
using mfRCF.Core;
using mfRCF.Core.Extensions;
using mfRCF.Rss;
using Microsoft.SPOT;
using mfXmlSerializer;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace mfRCF.NetduinoPlus2.TestApp
{
    public class OnboardLedGetCommand : mfRcfCommand
    {
        static OutputPort _onboardLed = new OutputPort(Pins.ONBOARD_LED, false);

        public OnboardLedGetCommand(string httpPath)
            : base(httpPath, new mfGETMethod()) 
        {
        }


        public override void ExecuteCommand(mfIHttpContext httpContext)
        {
            //e.g.: http://192.168.70.106/led?state=true

            var parameters = httpContext.Request.RawUrl.ExtractUrlParameter();

            if (!parameters.Contains("state"))
            {
                Debug.Print(GetType() + " no led-state parameter found");
            }

            if (parameters["state"].ToString().ToLower() == "true")
            {
                _onboardLed.Write(true);
            }
            else if (parameters["state"].ToString().ToLower() == "false")
            {
                _onboardLed.Write(false);
            }

            //write response
            httpContext.Response.StatusCode = enumHttpStatusCode.OK;

            httpContext.Response.Close();
            Debug.Print(GetType() + " executed, " + Debug.GC(false) + " bytes free");

        }
    }
}
